package cn.tedu.cloudcar01.controller;

import cn.tedu.cloudcar01.base.response.JsonResult;
import cn.tedu.cloudcar01.base.response.StateCode;
import cn.tedu.cloudcar01.mapper.UserMapper;
import cn.tedu.cloudcar01.pojo.dto.UserLoginParam;
import cn.tedu.cloudcar01.pojo.dto.UserRegParam;
import cn.tedu.cloudcar01.pojo.entiry.User;
import cn.tedu.cloudcar01.pojo.vo.CurrentUserQuery;
import cn.tedu.cloudcar01.pojo.vo.UserQuery;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
@Slf4j
@RestController
@RequestMapping("/v1/user/")
@Api(tags = "01.用户管理")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    //添加用户的功能
    @ApiOperation("用户注册")
    @ApiOperationSupport(order = 10)
    @PostMapping("reg")
    public JsonResult add(@RequestBody UserRegParam userRegParam){
        log.debug("userRegParam:"+userRegParam);
        String username = userRegParam.getUsername();
        UserQuery userQuery= userMapper.selectByUsername(username);
        if (userQuery!=null){
            return new JsonResult(StateCode.USERNAME_EXISTS);
        }
        User user = new User();
        BeanUtils.copyProperties(userRegParam,user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        userMapper.reg(user);
        return JsonResult.ok();
    }
    //用户登录功能
    @PostMapping("login")
    @ApiOperation("用户登录")
    @ApiOperationSupport(order = 20)
    public JsonResult login(@RequestBody UserLoginParam userLoginParam ,@ApiIgnore HttpSession session){
        //要去查找是否输入正确
        String username = userLoginParam.getUsername();
        String password = userLoginParam.getPassword();
        UserQuery userQuery=userMapper.login(username,password);
        if (userQuery==null){
            return new JsonResult(StateCode.USERNAME_PASSWORD_ERROR);
        }
        session.setAttribute("user",userQuery);
        return JsonResult.ok();
    }
    //获取当前用户，要查看是否确实已经登录了
    @ApiOperation("当前用户")
    @ApiOperationSupport(order = 30)
    @GetMapping("currentUser")
    public JsonResult currentUser(@ApiIgnore HttpSession session){
        UserQuery userQuery = (UserQuery ) session.getAttribute("user");
        if (userQuery==null){
            return new JsonResult(StateCode.NOT_LOGIN);
        }
        String username = userQuery.getUsername();
        CurrentUserQuery currentUser=userMapper.currentUser(username);
        return JsonResult.ok(userQuery);
    }
    //退出登录
    @ApiOperation("退出登录")
    @ApiOperationSupport(order = 40)
    @GetMapping("logout")
    public void logout(@ApiIgnore HttpSession session){
        session.removeAttribute("user");
    }
}














